package org.csu.webjpetstoreback.controller;

import jakarta.servlet.http.HttpSession;
import org.csu.webjpetstoreback.config.CommonResponse;
import org.csu.webjpetstoreback.model.Product;
import org.csu.webjpetstoreback.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.List;

@RestController
@SessionAttributes(names = "httpSession")
@Validated
public class SearchController {

    @Autowired
    public ProductService productService;


    @GetMapping("/catalog/search/{keyWord}")
    public CommonResponse<List<Product>> setSearchResult(@PathVariable("keyWord")String keyWord,HttpSession session){
        session.setAttribute("keyWord",keyWord);
        System.out.println("keyWord"+keyWord);
        return CommonResponse.BackForSuccess("保存关键字成功");
    }

    @GetMapping("/catalog/search")
    public CommonResponse<List<Product>> getSearchResult(HttpSession session){
        String keyWord= (String) session.getAttribute("keyWord");
        return productService.GetProductsListByCategory(keyWord);
    }

}
